﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JWUX.Framework.Extension;
using JWUX.Supermarket.Utility;

namespace JWUX.Supermarket.Winform
{
    public partial class frmRequest : Form
    {
        #region 成员

        /// <summary>
        /// Request列表
        /// </summary>
        private Model.RequestsCollection SENT_REQUESTS = new Model.RequestsCollection();

        /// <summary>
        /// 当前站位信息
        /// </summary>
        private Model.StationInfo CURRENT_LOCATION = new Model.StationInfo();

        /// <summary>
        /// 当前用户信息
        /// </summary>
        private Model.UserInfo CURRENT_USER = new Model.UserInfo();

        /// <summary>
        /// 临时存放Model.RequestInfo实例
        /// </summary>
        private Model.RequestInfo tempRequest = new Model.RequestInfo();

        #endregion

        #region 构造和方法

        public frmRequest()
        {
            InitializeComponent();

            SENT_REQUESTS.CountChanged += new Model.RequestsCollection.CountChangedEventHandler(SENT_REQUESTS_CountChanged);

            this.dgvDetail.AutoGenerateColumns = false;
            this.dgvStatus.AutoGenerateColumns = false;
        }

        void SENT_REQUESTS_CountChanged(object sender, Model.RequestsCountChangeEventArgs e)
        {
        }

        /// <summary>
        /// 创建物料申请单
        /// </summary>
        /// <param name="modelId">产品Id</param>
        /// <param name="outputQty">产品套数</param>
        /// <param name="requireTime">需求时间</param>
        /// <param name="creator">申请者实例</param>
        /// <param name="station">站位实例</param>
        /// <returns></returns>
        private Model.RequestInfo CreateRequest(
            decimal modelId,
            int outputQty,
            int requireTime,
            Model.UserInfo creator,
            Model.StationInfo station)
        {
            if (creator == null)
                throw new ArgumentNullException("creator");
            if (station == null)
                throw new ArgumentNullException("station");

            //获取BOM信息
            Model.BOMInfo bom = BLL.BOM.Get(modelId);

            //获取此站位上需要的物料信息
            List<Model.MaterialItem> materials = bom.FindLocation((int)station.Id);

            Model.RequestInfo newRequest = new Model.RequestInfo();

            newRequest.BillId = null;
            newRequest.CreatedBy = creator.FullName;
            newRequest.CreatedById = creator.Id;
            newRequest.Id = null;
            newRequest.IsCancelled = false;
            newRequest.LocationId = (int)station.Id;
            newRequest.Materials = materials;
            newRequest.ModelId = modelId;
            newRequest.Number = outputQty;
            newRequest.Requestor = creator.FullName;
            newRequest.RequestorId = (int)creator.Id;
            newRequest.RequireTime = requireTime;

            return newRequest;
        }

        #endregion

        #region 事件

        private void btnGetDetail_Click(object sender, EventArgs e)
        {
            //tempRequest = CreateRequest(
            //    (decimal)this.cbxModel.SelectedValue,
            //    (int)this.nudNumber.Value,
            //    (int)this.numRequireTime.Value,
            //    CURRENT_USER,
            //    CURRENT_LOCATION
            //);
            tempRequest.Materials.Add(new Model.MaterialItem { Id = 1, PartNumber = "L11111", Description = "AAAAA" });
            this.dgvDetail.DataSource = tempRequest.Materials;
            this.dgvDetail.Refresh();
        }

        private void btnSendRequest_Click(object sender, EventArgs e)
        {
            /*
            //将RequestInfo插入数据库
            decimal newId = BLL.Request.Insert(tempRequest);
            if (newId > 0) //获取插入后的Id，判断是否插入成功
            {
                this.SENT_REQUESTS.Add(tempRequest.BillId, tempRequest); //将request添加到字典
            }
             * */
            this.SENT_REQUESTS.Add("AAAA", tempRequest);
        }

        #endregion

        private void frmRequest_Load(object sender, EventArgs e)
        {
            this.lblBayName.Text = "线别：{0}".FormatWith(CURRENT_LOCATION.Bay.Name);
            this.lblStationName.Text = "工位：{0}".FormatWith(CURRENT_LOCATION.Name);
        }
    }
}
